﻿using System;
using System.Globalization;
using System.Web.Compilation;
using System.Resources;

using BizElements.BusinessLayer;

namespace BizElements.Web
{
    /// <summary>
    /// Uses underlying <see cref="DbTextSource"/> object to provide resources.
    /// </summary>
    internal class DbResourceProvider : IResourceProvider
    {
        DbTextSource textSource;

        /// <summary>
        /// Initializes a new instance of <b>DbResourceProvider</b> class.
        /// </summary>
        /// <param name="textSource">Text source that reads resources from database.</param>
        public DbResourceProvider(DbTextSource textSource)
        {
            this.textSource = textSource;
        }

        /// <summary>
        /// Returns a resource object for the key and culture.
        /// </summary>
        /// <param name="resourceKey">The key/name identifying a particular resource.</param>
        /// <param name="culture">The culture identifying a localized value for the resource. If <b>null</b> then <see cref="CultureInfo.CurrentUICulture"/> is used.</param>
        /// <returns>An <see cref="String"/> that contains the resource value for the resourceKey and culture.</returns>
        public object GetObject(string resourceKey, CultureInfo culture)
        {
            if (culture == null)
                culture = CultureInfo.CurrentUICulture;

            return this.textSource.GetString(resourceKey, culture);
        }

        /// <summary>
        /// Gets <see cref="DictionaryResourceKeysReader"/> object which enumerates all resource keys/names in the underlying <see cref="DbTextSource"/> object.
        /// </summary>
        public IResourceReader ResourceReader
        {
            get { return new DictionaryResourceKeysReader(this.textSource.GetKeys()); }
        }
    }
}
